package cn.edu.usst.cs.goldfish.booking.meetingroom.config;

import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.javamail.*;

import java.lang.reflect.Field;

@Slf4j
@Configuration
@ConfigurationProperties(prefix = "spring.mail")
public class EmailConfig {

    @Value("${spring.mail.username}")
    private String username;
    @Value("${spring.mail.host}")
    private String host;
    @Value("${spring.mail.password}")
    private String password;
    @Value("${spring.mail.properties.smtp.ssl.enable}")
    private boolean ssl;
    @Value("${spring.mail.properties.smtp.auth}")
    private boolean smtp;
    @PostConstruct
    public void init() {
        for(Field field : this.getClass().getDeclaredFields()){
            try {
                field.setAccessible(true);
                log.info("{}: {}", field.getName(), field.get(this));
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }
    @Bean
    public JavaMailSenderImpl javaMailSender() {
        JavaMailSenderImpl mail = new JavaMailSenderImpl();
        mail.setHost(host);
        mail.setUsername(username);
        mail.setPassword(password);
//        mail.setJavaMailProperties(props);
        mail.setDefaultEncoding("UTF-8");
        mail.setProtocol(ssl ? "smtps" : "smtp");
//        mail.setJavaMailProperties(props);
        return mail;
    }
}
